import pymysql
import requests


class OaLogAPI:
    def addlog(self):
        # 登录
        my = {"username": "19515542478", "password": "123456"}
        a = requests.post("http://192.168.124.121:8080/login", data=my)
        # print(a.json())
        # 提取token
        rdata = a.json()
        t = rdata["Admin-Token"]
        # t=rdata.get("Admin-Token")
        token = {"Admin-Token": t}

        # 文件上传
        msgdata = {"type": "img"}
        f1 = open(r"C:\Users\立哲\Pictures\Saved Pictures\01.jpg", "rb")
        msgfile = {"file": ("01.jpg", f1, "image/jpg")}
        a = requests.request("post", "http://192.168.124.121:8080/file/upload", data=msgdata, files=msgfile,headers=token)
        # print(a.json())

        # 获取batchId
        rrdata = a.json()
        t1 = rrdata["batchId"]
        batchId = {"batchId": t1}

        # 新增日志
        add = {
            "categoryId": "1",
            "content": "今日工作内容~",
            "tomorrow": "明日工作计划~",
            "question": "今日遇到的问题~",
            "batchId": batchId,
            "sendUserIds": "2,3",
            "sendDeptIds": "",
            "customerIds": "",
            "contactsIds": "",
            "businessIds": "",
            "contractIds": ""
        }
        a = requests.post("http://192.168.124.121:8080/OaLog/addOrUpdate", json=add, headers=token)
        # print(a.json())
        return a.json()["code"]
    def updataLog(self):
        # 登录
        my = {"username": "19515542478", "password": "123456"}
        a = requests.post("http://192.168.124.121:8080/login", data=my)
        # print(a.json())
        # 提取token
        rdata = a.json()
        t = rdata["Admin-Token"]
        # t=rdata.get("Admin-Token")
        token = {"Admin-Token": t}

        # 修改日志
        x = {
            "logId": "1",
            "categoryId": "1",
            "content": "这里是今日内容的修改",
            "tomorrow": "这里明日工作计划的修改",
            "question": "这里是今日遇到的问题的修改",
            "batchId": "",
            "sendUserIds": "2,3",
            "sendDeptIds": "",
            "customerIds": "",
            "contactsIds": "",
            "businessIds": "",
            "contractIds": ""
        }
        xg = requests.post("http://192.168.124.121:8080/OaLog/addOrUpdate", headers=token, json=x)
        return xg.json()["code"]
        # print(xg.json())

    def deleteLog(self):
        # 登录
        my = {"username": "19515542478", "password": "123456"}
        a = requests.post("http://192.168.124.121:8080/login", data=my)
        # print(a.json())
        # 提取token
        rdata = a.json()
        t = rdata["Admin-Token"]
        # t=rdata.get("Admin-Token")
        token = {"Admin-Token": t}

        # 删除日志
        # 连接数据库
        databases = pymysql.connect(user='root', password='123456', host='192.168.124.121', database='crm9')
        # 创建游标对象
        cursor = databases.cursor()
        # 编写sql语句并执行
        cursor.execute("SELECT log_id from 72crm_oa_log where create_user_id =9;")
        databases.commit()
        # 所有行查看
        dataaa = cursor.fetchall()
        print(dataaa)

        # 删除留言
        for i in dataaa:
            logg = {"logId": i[0]}
            requests.post("http://192.168.124.121:8080/OaLog/deleteById", data=logg, headers=token)

        sql = cursor.execute("SELECT log_id from 72crm_oa_log where create_user_id =9;")
        dataaa = cursor.fetchall()
        print(dataaa)
        if sql == 0:
            aa="删除成功"
            print(aa)
            return aa
    def selectLog(self):
        # 登录
        my = {"username": "19515542478", "password": "123456"}
        a = requests.post("http://192.168.124.121:8080/login", data=my)
        # print(a.json())
        # 提取token
        rdata = a.json()
        t = rdata["Admin-Token"]
        # t=rdata.get("Admin-Token")
        token = {"Admin-Token": t}

        # 查询日志
        z = {"page": 1, "limit": 5, "by": "1"}
        a = requests.post("http://192.168.124.121:8080/OaLog/queryList", data=z, headers=token)
        print(a.json())
        return a.json()["code"]